package com.joymeter.common.utils;

import java.math.BigDecimal;

/**
 * @ClassName Compare
 * @Description TODO
 * @Author liang
 * @Date 2018/8/17 9:52
 * @Version 1.0
 **/
public class Compare {

    /**
     * 比较两个字符类型的数字大小
     * str1 > str2  返回1
     * str1 < str2  返回-1
     * str1 = str2  返回0
     * @param str1
     * @param str2
     * @return
     */
    public static int compareValue(String str1,String str2){
        int temp1 = 0;
        int temp2 = 0;
        int len1 = str1.length();
        int len2 = str2.length();
        int i = 0;
        int j = 0;
        while (i < len1 || j < len2){
            temp1 = 0;
            temp2 = 0;
            while (i < len1 && str1.charAt(i) != '.'){
                temp1 = temp1*10 + str1.charAt(i++)-'0';
            }
            while (j < len2 && str2.charAt(j) != '.'){
                temp2 = temp2*10 + str2.charAt(j++)-'0';
            }
            if(temp1 > temp2) return 1;
            else if (temp1<temp2) return -1;
            else {
                i++;
                j++;
            }
        }
        return 0;
    }

    /**
     * 计算两个字符串的差值
     * @param str1
     * @param str2
     * @return
     */
    public static String calculateString(String str1 , String str2){
        BigDecimal a = new BigDecimal(str1);
        BigDecimal b = new BigDecimal(str2);
        String result = b.subtract(a).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
        return result;
    }
}
